unit _TSoundEngine;

interface
uses
  classes,udxaudio, config;

type

   TSoundEngine=class
   private
     Collection : TSoundCollection;
     Track : TSoundItem;

   public
     constructor Create();
     function loadTrack(filename : string) : integer;
     procedure unloadTrack(id : integer);
     procedure play(id : integer);
     procedure stop(id : integer);
     function nbTrack() : integer;
     destructor Destroy;
   end;

implementation

constructor TSoundEngine.Create();
  begin
    self.Collection:=TSoundCollection.Create(TSoundItem);
    Track:=nil;
  end;

function TSoundEngine.loadTrack(filename : string) : integer;
begin
  Track:=Collection.Add as TSoundItem;
  Track.LoadFromFile(SoundDirectory+filename);

  result:=Track.ID;
end;

procedure TSoundEngine.unloadTrack(id : integer);
begin
   Track:=Collection.FindItemID(id) as TSoundItem;
   Track.Destroy;
   Track:=nil;
end;

procedure TSoundEngine.play(id : integer);
begin
   Track:=Collection.FindItemID(id) as TSoundItem;
   if Track<>nil then
      Track.Play;
end;

procedure TSoundEngine.stop(id : integer);
begin
   Track:=Collection.FindItemID(id) as TSoundItem;
   if Track.IsPlaying then
      Track.Stop;
end;

function TSoundEngine.nbTrack() : integer;
begin
  result:=Collection.Count;
end;

destructor TSoundEngine.Destroy();
  begin
    self.Collection.Destroy;
    Track:=nil;
  end;
end.
